<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" center destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item :label="$t('user_data.head')" prop="head">
				<sc-upload v-model="form.head" :title="$t('user_data.headText')"></sc-upload>
			</el-form-item>
			<el-form-item :label="$t('user_data.background')" prop="background">
				<sc-upload v-model="form.background" :title="$t('user_data.backgroundText')"></sc-upload>
			</el-form-item>
			<el-form-item :label="$t('user_data.userName')" prop="userName">
				<el-input v-model="form.userName" clearable></el-input>
			</el-form-item>				
			<el-form-item :label="$t('user_data.fullName')" prop="fullName">
				<el-input v-model="form.fullName" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('user_data.userTypeLabel')" prop="userType">
				<el-input v-model="form.userType" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('user_data.sexLabel')" prop="sex">
				<el-radio-group v-model="form.sex">
					<el-radio :label="$t('user_data.sex.male')">{{$t('user_data.sex.male')}}</el-radio>
					<el-radio :label="$t('user_data.sex.female')">{{$t('user_data.sex.female')}}</el-radio>
					<el-radio :label="$t('user_data.sex.other')">{{$t('user_data.sex.other')}}</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item :label="$t('user_data.desc')" prop="desc">
				<el-input type="textarea" rows="5" resize="none" v-model="form.desc" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('user_data.location')" prop="location">
				<el-select v-model="form.location" filterable :placeholder="$t('table_data.selectText')">
					<el-option
						v-for="item in locationArr"
						:key="item.value"
						:label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item :label="$t('user_data.email')" prop="email">
				<el-input v-model="form.email" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('user_data.phoneNo')" prop="phoneNo">
				<el-input v-model="form.phoneNo" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('user_data.statusLabel')" prop="status">
				<el-checkbox-group v-model="form.status">
					<el-checkbox-button :label="$t('user_data.status.type1')"></el-checkbox-button>
					<el-checkbox-button :label="$t('user_data.status.type2')"></el-checkbox-button>
					<el-checkbox-button :label="$t('user_data.status.type3')"></el-checkbox-button>
					<el-checkbox-button :label="$t('user_data.status.type4')"></el-checkbox-button>
				</el-checkbox-group>
			</el-form-item>
			<el-form-item label="Twitter" prop="twitter">
				<el-input v-model="form.twitter" clearable></el-input>
			</el-form-item>
			<el-form-item label="Instagram" prop="instagram">
				<el-input v-model="form.instagram" clearable></el-input>
			</el-form-item>
			<el-form-item label="Facebook" prop="facebook">
				<el-input v-model="form.facebook" clearable></el-input>
			</el-form-item>
			<!-- <el-form-item label="授权至" prop="exp">
				<el-date-picker v-model="form.exp" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
			</el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >{{$t('table_data.cancelText')}}</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">{{$t('table_data.saveText')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增APP',
					edit: this.$t('table_data.userTitle')
				},
				locationArr: [{
          value: 'ja',
          label: 'Japanese'
        }, {
          value: 'en',
          label: 'English'
        }, {
          value: 'chs',
          label: '中文简体'
        }, {
          value: 'cht',
          label: '中文繁體'
        }],
				form: {
					head: "",
					background: "",
					location: "",
					fullName:"",
					userType: "",
					userName: "",
					email: "",
					phoneNo: "",
					status: "",
					sex: 0,
					desc: '',
					twitter: '',
					instagram: '',
					facebook: '',
				},
				rules: {
					appId:[
						{required: true, message: '请输入应用标识'}
					],
					appName:[
						{required: true, message: '请输入应用名称'}
					],
					secret:[
						{required: true, message: '请输入秘钥'}
					],
					type:[
						{required: true, message: '请选择类型范围', trigger: 'change'}
					],
					exp:[
						{required: true, message: '请选择授权到期日期', trigger: 'change'}
					]
				},
				visible: false,
				isSaveing: false
			}
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.demo.post.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.appId = data.appId
				this.form.appName = data.appName
				this.form.secret = data.secret
				this.form.type = data.type
				this.form.exp = data.exp
			}
		}
	}
</script>

<style>
</style>
